package com.nantian.logic.controller.mainControl.memberControl;

import com.nantian.logic.entity.ImgAddressEntity.ImgAddressEntity;
import com.nantian.logic.repository.imgAddressRepository.ImgAddressRepository;
import com.nantian.logic.utils.DateUtils;
import com.nantian.logic.utils.FileUtils;
import com.nantian.logic.utils.TokenUtil;
import com.nantian.logic.utils.UniversalResult;
import com.nantian.security.repository.UserRepository;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.Random;

@Controller
public class memberController {
    @Autowired
    UserRepository userRepository;

    @Autowired
    ImgAddressRepository imgAddressRepository;
    @Autowired
    TokenUtil tokenUtil;

    //个人头像上传
    @PostMapping(value = "/uploadMemberHeadImg")
    @ResponseBody
    public UniversalResult upload(MultipartFile file, HttpServletRequest request) {
        String path = "memberImg";  //定义上传的图片保存的文件夹
        String uploadresult = FileUtils.uploadFile(file, path);
        String userId = tokenUtil.getUserId(request);
        if (uploadresult.equals("上传失败")) {
            return new UniversalResult<>(UniversalResult.FAILURE, "上传失败");
        } else {
            ImgAddressEntity imgAddressEntity1 = imgAddressRepository.findByUseridAndType(userId,"0");
            ImgAddressEntity imgAddressEntity = new ImgAddressEntity();
            if(imgAddressEntity1 == null ){
                String imgid = DateUtils.getyyyyMMddHHmmss() + new Random().nextInt(10);
                imgAddressEntity.setImgId(imgid);
            }
            else {
                imgAddressEntity.setImgId(imgAddressEntity1.getImgId());
            }
            imgAddressEntity.setUserid(userId);
            imgAddressEntity.setImgAddress(uploadresult);  //数据库中保存文件夹名+文件名
            imgAddressEntity.setUploadDate(DateUtils.getDate());
            imgAddressEntity.setType("0"); // 个人头像
            imgAddressEntity.setDel(Byte.valueOf("0"));

            return new UniversalResult<>(UniversalResult.SUCCESS, "上传成功");
        }
    }


    /**
     * 根据userid和图片type加载图片，以流的方式输出
     *
     * @param
     * @param response
     * @return
     */
    @GetMapping(value = "/general/downMemberHeadImg")
    public UniversalResult selectFarmImgByUid(HttpServletRequest request, @RequestParam("type") String
            type, HttpServletResponse response) {
        String userId = tokenUtil.getUserId(request);
        if (StringUtils.isBlank(userId)) {
            return new UniversalResult(UniversalResult.PARAM_ERROR, "用户id不能为空！");
        }
        ImgAddressEntity images = imgAddressRepository.findByUseridAndType(userId,type);
        if (images == null) {
            String path = System.getProperty("user.dir") + File.separator+ "img" + File.separator + "1.png";
            File file = new File(path);
            String info = "商品图片加载失败！";
            FileUtils.outputFileStream(file, info, path, response);
            System.out.println("加载图片路径："+path);
            return new UniversalResult(UniversalResult.FAILURE, "图片不存在！");
        }else{
            File file1 = new File(System.getProperty("user.dir"));
            // get parent dir
            String parentPath = file1.getParent();
            String path = parentPath + File.separator + images.getImgAddress();
            File file = new File(path);
            String info = "商品图片加载失败！";
            System.out.println("加载图片路径："+path);
            FileUtils.outputFileStream(file, info, path, response);
            return new UniversalResult<>(UniversalResult.SUCCESS, "商品图片加载成功！", path);
        }
    }
}
